/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.infor.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.infor.entity.Carrier;
import com.jeeplus.modules.infor.mapper.CarrierMapper;
import com.jeeplus.modules.infor.entity.CarrierPrice;
import com.jeeplus.modules.infor.mapper.CarrierPriceMapper;

/**
 * 承运商Service
 * @author 李超辉
 * @version 2017-11-23
 */
@Service
@Transactional(readOnly = true)
public class CarrierService extends CrudService<CarrierMapper, Carrier> {

	@Autowired
	private CarrierPriceMapper carrierPriceMapper;
	
	public Carrier get(String id) {
		Carrier carrier = super.get(id);
		carrier.setCarrierPriceList(carrierPriceMapper.findList(new CarrierPrice(carrier)));
		return carrier;
	}
	
	public List<Carrier> findList(Carrier carrier) {
		return super.findList(carrier);
	}
	
	public Page<Carrier> findPage(Page<Carrier> page, Carrier carrier) {
		return super.findPage(page, carrier);
	}
	
	@Transactional(readOnly = false)
	public void save(Carrier carrier) {
		super.save(carrier);
		for (CarrierPrice carrierPrice : carrier.getCarrierPriceList()){
			if (carrierPrice.getId() == null){
				continue;
			}
			if (CarrierPrice.DEL_FLAG_NORMAL.equals(carrierPrice.getDelFlag())){
				if (StringUtils.isBlank(carrierPrice.getId())){
					carrierPrice.setCarrier(carrier);
					carrierPrice.preInsert();
					carrierPriceMapper.insert(carrierPrice);
				}else{
					carrierPrice.preUpdate();
					carrierPriceMapper.update(carrierPrice);
				}
			}else{
				carrierPriceMapper.delete(carrierPrice);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Carrier carrier) {
		super.delete(carrier);
		carrierPriceMapper.delete(new CarrierPrice(carrier));
	}
	
}